﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MissionCenter
{
    /// <summary>
    /// ScriptList.xaml 的交互逻辑
    /// </summary>
    public partial class ScriptList : UserControl
    {
        public ScriptList()
        {
            InitializeComponent();
            this.DataContext = ScriptDateList;
        }
        /// <summary>
        /// 脚本列表数据绑定器
        /// </summary>
        public ScriptViewModel ScriptDateList=new ScriptViewModel();
        private void refresh_Click(object sender, RoutedEventArgs e)
        {
            //刷新脚本列表

        }

        private void test01_Click(object sender, RoutedEventArgs e)
        {
            string str = "m";//w:浏览器 m:手机 r:机器人 其它
            ScriptDateList.ScriptList.Add(new ScriptItem { 
                Name = "Script3", 
                TypeOfScript = str=="w"? "🌐" : str=="m"?"📱":str=="s"? "💻" : "🤖"
            });
            //ListBox滚到到最后一行
            scriptlistbox.ScrollIntoView(scriptlistbox.Items[scriptlistbox.Items.Count - 1]);
        }
    }

    public class ScriptItem : INotifyPropertyChanged
    {
        /// <summary>
        /// 列表显示的ID
        /// </summary>
        public int _id;
        /// <summary>
        /// 脚本的唯一ID
        /// </summary>
        public int _uid;
        /// <summary>
        /// 脚本的名称
        /// </summary>
        public string _name;
        /// <summary>
        /// 脚本的类型
        /// </summary>
        public string _typeofscript;




        /// <summary>
        /// 列表显示的ID
        /// </summary>
        public int Id
        {
            get { return _id; }
            set
            {
                _id = value;
                OnPropertyChanged(nameof(Id));
            }
        }

        /// <summary>
        /// 脚本的唯一ID
        /// </summary>
        public int Uid
        {
            get { return _uid; }
            set
            {
                _uid = value;
                OnPropertyChanged(nameof(Uid));
            }
        }

        /// <summary>
        /// 脚本的名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged(nameof(Name));
            }
        }
        /// <summary>
        /// 脚本的类型
        /// </summary>
        public string TypeOfScript
        {
            get { return _typeofscript; }
            set
            {
                _typeofscript = value;
                OnPropertyChanged(nameof(TypeOfScript));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }


    public class ScriptViewModel : INotifyPropertyChanged
    {
        private ObservableCollection<ScriptItem> _scriptList;
        private ScriptItem _selectedScript;

        public ObservableCollection<ScriptItem> ScriptList
        {
            get { return _scriptList; }
            set
            {
                _scriptList = value;
                OnPropertyChanged(nameof(ScriptList));
            }
        }
        public ScriptItem SelectedScript
        {
            get { return _selectedScript; }
            set
            {
                _selectedScript = value;
                OnPropertyChanged(nameof(SelectedScript));
            }
        }

        public ScriptViewModel()
        {
            // 初始化 ScriptList 和其他数据
            ScriptList = new ObservableCollection<ScriptItem>();
            int i = 0;
            ScriptList.CollectionChanged += (sender, e) =>
            {
              //如果是添加操作

              if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
                {

                    //遍历添加的项
                    foreach (ScriptItem item in e.NewItems)
                    {
                    //添加事件处理

                        i++;
                        item._id= i;
                    }
                }
            };
            //// 添加示例数据
            //ScriptList.Add(new ScriptItem { Name = "Script1", Version = "1.0" });
            //ScriptList.Add(new ScriptItem { Name = "Script2", Version = "2.0" });
            //// 其他初始化操作
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}
